package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
   private MenuService menuService;

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try{
            menuService.add(menu);//发送请求
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }


    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            menuService.delete(id);//发送请求
            return new Result(true, MessageConstant.DEL_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DEL_MENU_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        try{
            menuService.update(menu);//发送请求
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = menuService.findPage(queryPageBean);

        return page;
    }


    //查询父菜单
    @RequestMapping("/findparentMenuIds")
    public Result findparentMenuIds(){

        try {
            List<Menu> menus = menuService.findparentMenuIds();
            List<Map<String,Object>> list=new ArrayList<>();
            for (Menu menu : menus) {
                Map<String,Object> map=new HashMap<>();
                Integer id = menu.getId();
                String name = menu.getName();
                map.put("value",id);
                map.put("label",name);
                list.add(map);
            }
            return new Result(true,MessageConstant.GET_MENUS_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENUS_FAIL);
        }

    }

    //查询ID数据回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu=menuService.findById(id);
            return new Result(true,MessageConstant.GET_MENUID_SUCCESS,menu);
            }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENUID_FAIL);
        }

    }


}
